package cn.edu.ctbu.studentstest.webapi;

import cn.edu.ctbu.studentstest.core.PageUtils;
import cn.edu.ctbu.studentstest.domain.Students;
import cn.edu.ctbu.studentstest.service.StudentService;
import net.bytebuddy.TypeCache;
import org.dom4j.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/webapi/students")
public class StudentRestController {

    @Autowired
    private StudentService studentService;

    /**
     * 获取全部学生的id，/webapi/students/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public Students get(@PathVariable Long id){
        Students students=studentService.getById(id);

        students.setPassword("");
        return students;
    }

    /**
     * 分页，读取全部内容/webapi/students/getbypage
     * @return
     */
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "name",defaultValue = "") String name){
        //按id排倒序
        Sort sort= Sort.by(Sort.Direction.DESC,"id");
        //Pageable pageable=PageRequest.of(page,size,sort);
        Page<Students> studentsPage;

        if(name == null || name.equals("")){
            Pageable pageable=PageRequest.of(page,size,sort);
            studentsPage=studentService.findAll(pageable);
        }else{
            Students students=new Students();
            students.setName(name);

            Pageable pageable=PageRequest.of(0,10,sort);
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());//全部模糊查询
            Example<Students>example=Example.of(students,matcher);
            studentsPage=studentService.findAll(example,pageable);
        }
        PageUtils pageUtils=new PageUtils(studentsPage.getContent(),studentsPage.getTotalElements());
        return pageUtils;
    }
    /**
     * 获取全部学生的信息，/webapi/students/list
     * @return
     */
    @GetMapping("/list")
    public List<Students> getall(){
        List<Students> students=studentService.findAll();
        return students;
    }
    /**
     * 新增学生的方法，/webapi/students/insert
     * @return
     */
    @PostMapping("/insert")
    public Students insert(Students student){
        Students student1=studentService.insert(student);
        return student1;
    }

    /**
     * 更新学生的方法，/webapi/students/update
     * @return
     */
    @PutMapping("/update")
    public Students updata(Students student){

        //读取旧的数据
        Students oldstudent=studentService.getById(student.getId());

        if(student.getPassword()==null||"".equals(student.getPassword())){
            //没有新密码，用旧密码
            student.setPassword(oldstudent.getPassword());
        }
        student=studentService.upData(student);
        return student;
    }
    /**
     * 删除学生的方法，/webapi/students/delete
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public void delet2(@PathVariable Long id){
        studentService.delete2(id);
    }

    /**
     * 读取url，/webapi/students/findByName
     * @return
     */
    @GetMapping("/findByName")
   public  List<Students> findByStudentName(String name) {
        List<Students> all = new ArrayList<>();
        if (name == null || name.equals("")) {
            all = studentService.findAll();
        } else {
            all = studentService.findByStudentName(name);
        }
        return all;
    }


}
